package com.yuexunit.mobilenurse.module.BloodManage.model.impl;

import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodManage.Api.BloodApplyApi;
import com.yuexunit.mobilenurse.module.BloodManage.bean.CheckDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.model.ICheckModel;
import com.yuexunit.mobilenurse.util.ApiUtil;
import com.yuexunit.mobilenurse.util.CreateJson;
import com.yuexunit.mobilenurse.util.ProUtil;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.slf4j.Logger;

import java.util.Map;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by work-jx on 2017/10/24.
 */
public class CheckModel implements ICheckModel{
    private BloodApplyApi api;

    //记录Log
    private final Logger log = ProUtil.getLogger(CheckModel.class);

    public BloodApplyApi ApiInstance() {
        if (api != null) {
            return api;
        } else {
            return ApiUtil.getInstance().createRetrofitApi_BloodManage(BloodApplyApi.class);
        }
    }

    @Override
    public Observable<CheckDataBean> checkBlood(Map<String, String> praise) {
        api = ApiInstance();
        return api.checkBlood(praise);
    }

    @Override
    public Observable<CheckDataBean> beginBlood(Map<String, String> praise) {
        api = ApiInstance();
        return api.beginBlood(praise);
    }

    @Override
    public Observable<CheckDataBean> beginConfirmBlood(Map<String, String> praise) {
        api = ApiInstance();
        return api.beginConfirmBlood(praise);
    }

    @Override
    public Observable<CheckDataBean> finishBlood(Map<String, String> praise) {
        api = ApiInstance();
        return api.finishBlood(praise);
    }

    @Override
    public Observable<CheckDataBean> finishConfirmBlood(Map<String, String> praise) {
        api = ApiInstance();
        return api.finishConfirmBlood(praise);
    }

    @Override
    public Observable<CheckDataBean> cancle(Map<String, String> praise) {
        api = ApiInstance();
        return api.cancle(praise);
    }

    //查询账号密码是否能登陆
    @Override
    public Observable<Object> doInLogin(final String uid, final String pwd) {

        return Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                String nameSpace = AppConfig.WEB_NAME_SPACE;
                String methodName = AppConfig.WEB_NAME_METHOD;
                String url = AppConfig.WEB_CONTENT;
                SoapObject soapObject = new SoapObject(nameSpace, methodName);
                soapObject.addProperty("XmlString", CreateJson.Login_Json(uid, pwd));
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true; //very important for compatibility
                envelope.bodyOut = soapObject;

                HttpTransportSE htSE = new HttpTransportSE(url);
                Object response = null;
                try {

                    htSE.call(nameSpace + methodName, envelope);
                    response = envelope.getResponse();
                    //记录传输到服务器及传回的数据
                    log.error(CreateJson.LOG_JSON(url + ",mhs_login", "2", uid + "," + pwd, response.toString()));

                } catch (Exception e) {
                    log.error("doInLogin:" + e);
                    e.printStackTrace();
                    subscriber.onError(e);
                }
                subscriber.onNext(response);
                subscriber.onCompleted();
            }
        });
    }
}
